/**
 * Created by user on 2015/9/14.
 */

var lotteryService = require('../../../services/mis/operate/lotteryService');
var messageUtil = require('../../../common/utils/messageUtil');
var toolUtil = require('../../../common/utils/toolUtil');


exports.lotteryList = function(req, res) {

    lotteryService.lotteryListService(req, function(err, results) {

        if (err) {
            logger.error('Exception happen when lotteryList...' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happen when lotteryList...'));
        }else {
            var data = {
                count:results[2]["count"],
                totalPage: results[2]["totalPage"],
                list: results[1]
            };
            res.json(messageUtil.composeSucceededMessage(req, null, data));
        }

    });
};

exports.lotteryDelete = function(req, res) {

    lotteryService.lotteryDeleteService(req, function(err, results) {

        if (err) {
            logger.error('Exception happen when lotteryList...' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happen when lotteryList...'));
        }else {
            var data = {};
            res.json(messageUtil.composeSucceededMessage(req, null, data));
        }

    });
};

exports.getPrizeByLotteryId = function (req, res) {
    var input = req.body;
    lotteryService.getPrizeByLotteryIdService(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when getPrizeByLotteryId ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            var data = {
                list: result[0],
                count:result[1]["count"],
                totalPage: result[1]["totalPage"]
            };
            res.json(messageUtil.composeSucceededMessage(req, null, data));
        }
    })
};

exports.lotteryPrizeAdd = function (req, res) {

    toolUtil.uploadImageService(null, null, req, function (err, images) {
        if (err) {
            logger.error('addlotteryPrize - uploadImage - ' + err.message);
            res.json(messageUtil.composeFailedMessage(req, '奖品创建失败'));
        } else {
            var input = req.body;
            input.prizePic = images[0];
            lotteryService.lotteryPrizeAddService(input, function (err, result) {
                if (err) {
                    logger.error('Exception happened when addPrize ...!' + err.message);
                    res.json(messageUtil.composeFailedMessage(req, err.message));
                } else {
                    res.json(messageUtil.composeSucceededMessage(req, null, result));
                }
            })
        }
    });
};


exports.lotteryPrizeDelete = function(req, res) {

    lotteryService.lotteryPrizeDeleteService(req, function(err, results) {

        if (err) {
            logger.error('Exception happen when userDelete...' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happen when userDelete...'));
        }else {
            var data = {};
            res.json(messageUtil.composeSucceededMessage(req, null, data));
        }

    });
};


exports.lotteryPrizeEdit = function(req, res) {

    var input = req.body;

    if(req.files){
        toolUtil.uploadImageService(null, null, req, function (err, images) {
            if (err) {
                logger.error('updateLotteryPrize - uploadImage - ' + err.message);
                res.json(messageUtil.composeFailedMessage(req, '奖品创建失败'));
            } else {
                input.prizePic = images[0];
                lotteryService.lotteryPrizeEditService(input, function (err, result) {
                    if (err) {
                        logger.error('Exception happened when updateLotteryPrize ...!' + err.message);
                        res.json(messageUtil.composeFailedMessage(req, err.message));
                    } else {
                        var data = {};
                        res.json(messageUtil.composeSucceededMessage(req, null, data));
                    }
                })
            }
        })

    }else{
        lotteryService.lotteryPrizeEditService(input, function (err, result) {
            if (err) {
                logger.error('Exception happened when updateLotteryPrize ...!' + err.message);
                res.json(messageUtil.composeFailedMessage(req, err.message));
            } else {
                var data = {};
                res.json(messageUtil.composeSucceededMessage(req, null, data));
            }
        });
    }

};